//控制层 
app.controller('productCateController', function($scope, $controller,$location, productCateService) {

	$controller('baseController', {
		$scope: $scope
	}); //继承

	//读取列表数据绑定到表单中  
	$scope.findAll = function() {
		productCateService.findAll().success(
			function(response) {
				$scope.list = response;
			}
		);
	}

	//分页
	$scope.findPage = function(page, rows) {
		productCateService.findPage(page, rows).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	//查询实体 
	$scope.findOne = function(id) {
		productCateService.findOne(id).success(
			function(response) {
				$scope.entity = response;
			}
		);
	}

	//保存 
	$scope.save = function() {
		var serviceObject; //服务层对象  				
		if($scope.entity.id != null) { //如果有ID
			serviceObject = productCateService.update($scope.entity); //修改  
		} else {
			serviceObject = productCateService.add($scope.entity); //增加 
		}
		serviceObject.success(
			function(response) {
				if(response.success) {
					//重新查询 
					window.location = "product-category.html";
				} else {
					alert(response.message);
				}
			}
		);
	}

	//单个删除
	$scope.delsFlag = false;
	$scope.delsingle = function(id) {
		if(confirm('确实要删除该分类吗?')) {
			productCateService.dele(id).success(
				function(response) {
					if(response.success) {
						alert(response.message);
						$scope.reloadList();
						$scope.delsFlag = true;
						$timeout(function() {
							$scope.delsFlag = false;
						}, 2000);
					} else {
						alert(response.message);
					}
				}
			);
		}

	}

	//批量删除 
	$scope.dele = function() {
		//获取选中的复选框			
		productCateService.dele($scope.selectIds).success(
			function(response) {
				if(response.success) {
					$scope.reloadList(); //刷新列表
					$scope.selectIds = [];
				}
			}
		);
	}

	$scope.searchEntity = {}; //定义搜索对象 

	//搜索
	$scope.search = function(page, rows) {
		productCateService.search(page, rows, $scope.searchEntity).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	//编辑页面携带参数
	$scope.edit = function() {
		var id = $location.absUrl().split("?id=")[1];
		$scope.findOne(id);
	}

});